package zzs.generalframework.console.core.authorization.authrole.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import zzs.generalframework.common.core.authorization.authrole.AuthRoleInfo;
import zzs.generalframework.common.core.authorization.authrole.AuthRolePO;
import zzs.generalframework.console.core.authorization.authrole.dao.IAuthRoleDao;
import zzs.generalframework.console.util.QueryParameter;
import zzs.generalframework.util.core.string.StringUtils;

/**
 * @title
 * @description
 * @project zzs-console
 * @package zzs.generalframework.console.core.authorization.authrole.dao.impl
 * @author zzs
 * @date 2018年2月1日
 * @version 1.0
 */
@Repository("zzs.generalframework.console.core.authorization.authrole.dao.impl.authRoleDaoImpl")
public class AuthRoleDaoImpl implements IAuthRoleDao
{
	private Logger logger = LoggerFactory.getLogger(AuthRoleDaoImpl.class);
	@PersistenceContext(unitName = "zzs-entity")
	EntityManager entityManager;
	@Autowired
	JdbcTemplate jdbcTemplate;

	@Override
	public boolean insert(AuthRolePO authRolePO)
	{
		logger.info("====插入权限角色数据开始====");
		logger.debug("====插入权限角色数据,入参:AuthRolePO=[{}]====", authRolePO);
		entityManager.persist(authRolePO);
		logger.debug("====插入权限角色数据,出参:boolean=[{}]====", true);
		logger.info("====插入权限角色数据结束====");
		return true;
	}

	@Override
	public boolean update(AuthRolePO authRolePO)
	{
		logger.info("====更新权限角色数据开始====");
		logger.debug("====更新权限角色数据,入参:AuthRolePO=[{}]====", authRolePO);
		entityManager.merge(authRolePO);
		logger.debug("====更新数据,出参:boolean=[{}]====", true);
		logger.info("====更新权限角色数据结束====");
		return true;
	}

	@Override
	public List<AuthRolePO> select(AuthRoleInfo authRoleInfo)
	{
		logger.info("====查询权限角色数据开始====");
		logger.debug("====查询权限角色数据,入参:AuthRoleInfo=[{}]====", authRoleInfo);
		List<AuthRolePO> returnList = null;
		StringBuffer sqlBuffer = new StringBuffer();
		QueryParameter parameter = new QueryParameter();
		sqlBuffer.append("select a from AuthRolePO a where 1=1 ");
		if (StringUtils.isNotNullAndBlank(authRoleInfo.getRoleID()))
		{
			sqlBuffer.append(" and a.roleID=?");
			parameter.putParameterObject(authRoleInfo.getRoleID());
		}
		if (StringUtils.isNotNullAndBlank(authRoleInfo.getRoleCode()))
		{
			sqlBuffer.append(" and a.roleCode=?");
			parameter.putParameterObject(authRoleInfo.getRoleCode());
		}
		if (StringUtils.isNotNullAndBlank(authRoleInfo.getRoleName()))
		{
			sqlBuffer.append(" and a.roleName=?");
			parameter.putParameterObject(authRoleInfo.getRoleName());
		}
		if (StringUtils.isNotNullAndBlank(authRoleInfo.getRoleDesc()))
		{
			sqlBuffer.append(" and a.roleDesc=?");
			parameter.putParameterObject(authRoleInfo.getRoleDesc());
		}
		if (StringUtils.isNotNullAndBlank(authRoleInfo.getCreateUser()))
		{
			sqlBuffer.append(" and a.createUser=?");
			parameter.putParameterObject(authRoleInfo.getCreateUser());
		}
		if (StringUtils.isNotNullAndBlank(authRoleInfo.getUpdateUser()))
		{
			sqlBuffer.append(" and a.updateUser=?");
			parameter.putParameterObject(authRoleInfo.getUpdateUser());
		}
		if (StringUtils.isNotNullAndBlank(authRoleInfo.getRecordStatus()))
		{
			sqlBuffer.append(" and a.recordStatus=?");
			parameter.putParameterObject(authRoleInfo.getRecordStatus());
		}
		logger.info("查询数据SQL:[{}]", sqlBuffer.toString());
		Query query = entityManager.createQuery(sqlBuffer.toString());
		parameter.setQueryParameter(query);
		returnList = query.getResultList();
		logger.debug("====查询权限角色数据,出参:List<AuthRolePO>=[{}]====", returnList);
		logger.info("====查询权限角色数据结束====");
		return returnList;
	}

	@Override
	public boolean delete(AuthRolePO authRolePO)
	{
		logger.info("====删除权限角色数据开始====");
		logger.debug("====删除权限角色数据,入参:AuthRolePO=[{}]====", authRolePO);
		entityManager.remove(authRolePO);
		logger.debug("====删除权限角色数据,出参:boolean=[{}]====", true);
		logger.info("====删除权限角色数据结束====");
		return true;
	}

	@Override
	public Long count(AuthRolePO authRolePO)
	{
		logger.info("====统计权限角色数据开始====");
		logger.debug("====统计权限角色数据,入参:AuthRolePO=[{}]====", authRolePO);
		Long returnLong = null;
		StringBuffer sqlBuffer = new StringBuffer();
		QueryParameter parameter = new QueryParameter();
		sqlBuffer.append("select count(*) from AuthRolePO a where 1=1 ");
		if (StringUtils.isNotNullAndBlank(authRolePO.getRoleID()))
		{
			sqlBuffer.append(" and a.roleID=?");
			parameter.putParameterObject(authRolePO.getRoleID());
		}
		if (StringUtils.isNotNullAndBlank(authRolePO.getRoleCode()))
		{
			sqlBuffer.append(" and a.roleCode=?");
			parameter.putParameterObject(authRolePO.getRoleCode());
		}
		if (StringUtils.isNotNullAndBlank(authRolePO.getRoleName()))
		{
			sqlBuffer.append(" and a.roleName=?");
			parameter.putParameterObject(authRolePO.getRoleName());
		}
		if (StringUtils.isNotNullAndBlank(authRolePO.getRoleDesc()))
		{
			sqlBuffer.append(" and a.roleDesc=?");
			parameter.putParameterObject(authRolePO.getRoleDesc());
		}
		if (StringUtils.isNotNullAndBlank(authRolePO.getCreateUser()))
		{
			sqlBuffer.append(" and a.createUser=?");
			parameter.putParameterObject(authRolePO.getCreateUser());
		}
		if (StringUtils.isNotNullAndBlank(authRolePO.getUpdateUser()))
		{
			sqlBuffer.append(" and a.updateUser=?");
			parameter.putParameterObject(authRolePO.getUpdateUser());
		}
		if (StringUtils.isNotNullAndBlank(authRolePO.getRecordStatus()))
		{
			sqlBuffer.append(" and a.recordStatus=?");
			parameter.putParameterObject(authRolePO.getRecordStatus());
		}
		logger.info("统计数据SQL:[{}]", sqlBuffer.toString());
		Query query = entityManager.createQuery(sqlBuffer.toString());
		parameter.setQueryParameter(query);
		returnLong = (Long) query.getSingleResult();
		logger.debug("====统计权限角色数据,出参:Long=[{}]====", returnLong);
		logger.info("====统计权限角色数据结束====");
		return returnLong;
	}

}
